Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'债项编号',dataIndex:'debtNo',width:100},
{header:'债项名称',dataIndex:'debtDesc',width:100},
{header:'抵质押品代码',dataIndex:'collateralId',width:100},
{header:'抵质押品名称',dataIndex:'collateralName',width:100},
{header:'抵质押品类型',dataIndex:'collateralType',width:100},
{header:'抵质押品当前评估价值',dataIndex:'nowValue',width:100}
]); 

var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'obligation/listDebtCollateral.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'debtNo',mapping:'debt.debtNo'},
{name:'debtDesc',mapping:'debt.debtDesc'},
{name:'collateralId',mapping:'collateral.collateralId'},
{name:'collateralName',mapping:'collateral.collateralName'}, 
{name:'collateralType',mapping:'collateral.collateralType'},  
{name:'nowValue',mapping:'collateral.nowValue'}
]) 
}); 
ds.load({params:{start:0,limit:10}}); 
var yesOrNoStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=YesNo',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
	});
var collateralStore = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'obligation/listCurrentCollateral.do',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'code'}, 
{name:'name'}
]),
autoLoad: true
}); 

var grid=new Ext.grid.GridPanel({ 
id:'contentList',
renderTo:"list", 
stripeRows:true,//斑马线效果
title:"当前债项-抵质押品列表", 
height:530, 
cm:cm,
store: ds, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds,   
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
{text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看/编辑',handler:checkDetail},
 '-',
 {text:'删除',handler:del}
 ]              
});

//新增表单
var addForm = new Ext.form.FormPanel({ 
id: 'addForm', 
width: 700, 
height: 350, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 160, 
items: 
[ { xtype: 'fieldset', 
title: '抵质押品信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[
{layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'combo', 
		id: 'cId', 
		name: 'cId', 
        store :collateralStore,
        fieldLabel: '引入已有的抵质押品',
        valueField : 'code',
        displayField : 'name',
        emptyText : '请选择抵质押品...',
        allowBlank : true,
       // editable: false,
        typeAhead : true,
        width: 150,
        mode:'local', 
        hiddenName:'cId',
        triggerAction : 'all',
        forceSelection:true, 
		typeAhead:true, 
        selectOnFocus : true,
		listeners:{  select : function(combo, record,index){
					Ext.Ajax.request({
					  url:'obligation/getCollateral.do',
					  params : {collateralId : combo.value},
					  method:'POST',
					  success:function(response){
						  var json = Ext.util.JSON.decode(response.responseText);
						  Ext.getCmp("collateralId").setValue(json.collateralId);
						  Ext.getCmp("collateralName").setValue(json.collateralName);
						  Ext.getCmp("collateralTypeLevelOneName").setValue(json.collateralTypeLevelOneName);
						  Ext.getCmp("collateralTypeLevelTwoName").setValue(json.collateralTypeLevelTwoName);
						  Ext.getCmp("collateralTerm").setValue(json.collateralTerm);
						 // alert(json.endTerm);
						  Ext.getCmp("endDate").setValue(json.endTerm);
						  Ext.getCmp("nowValue").setValue(json.nowValue);
						  Ext.getCmp("loanValueDate").setValue(json.loanValueDate);
						  Ext.getCmp("loanValue").setValue(json.loanValue);
						  Ext.getCmp("dealValueDate").setValue(json.dealValueDate);
						  Ext.getCmp("dealValue").setValue(json.dealValue);
						  Ext.getCmp("recoverDate").setValue(json.recoverDate);
						  	Ext.getCmp("bankShare").setValue(json.bankShare);
						  	Ext.getCmp("recoverValue").setValue(json.recoverValue);
						  	Ext.getCmp("belongFlag").setValue(json.belongFlag);
						  Ext.getCmp("share").setValue("");
						   
					  },failure:function(){
					    Ext.MessageBox.alert("提示", "获取抵质押品信息失败！"); 
					  }
					});
				} 
		}
}
]},{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'button',
id: 'acBtn', 
name: 'acBtn', 
text: '点此新增一项抵质押品',
handler: addCol
}]} 
]},
{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'collateralId', 
name: 'collateralId', 
fieldLabel: '抵质押品代码',
allowBlank : false,
blankText:'请为当前债项选择抵质押品',
readOnly:true,
width: 150
}
]}
]},{
		layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
	        layout : "form",
	        items : [{
				xtype: 'textfield',
				id: 'collateralName', 
				name: 'collateralName', 
				fieldLabel: '抵质押品名称',
				readOnly:true,
				width: 150
	       }]
		}]
},{
layout:"column",
items:[{ columnWidth : .5, 
           layout : "form",
           items : [{
				xtype: 'textfield',
				id: 'collateralTypeLevelOneName', 
				name: 'collateralTypeLevelOneName', 
				fieldLabel: '抵质押品种类',
				readOnly:true,
				width: 150
           }]
}]
},{
	layout:"form",
	items:[{
		xtype: 'textfield',
		id: 'collateralTypeLevelTwoName', 
		name: 'collateralTypeLevelTwoName', 
		fieldLabel: '',
		readOnly:true,  
		width: 483
	}]
},
{
	layout:"column",
	items:[{ columnWidth : .5, 
	           layout : "form",
	           items : [{
	        	   xtype: 'textfield',
	   			id: 'collateralTerm', 
	   			name: 'collateralTerm', 
	   			fieldLabel: '抵质押品有效起始日',	   
	readOnly:true,
	width: 150 
	}]},
		{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	        	   xtype: 'textfield',
		   			id: 'endDate', 
		   			name: 'endDate', 
		   			fieldLabel: '抵质押品有效到期日',	   
					readOnly:true,
					width: 150 
	}
	]} 
	]},

{
layout:"column",
items:[
	{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'textfield',
        	   id: 'nowValue', 
        	   name: 'nowValue', 
        	   fieldLabel: '抵质押品当前评估价值（元）',
       		regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
     		regexText:'只能输入整数或小数！' , 
readOnly:true,  
width:150
}
]} 
]},{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
           xtype: 'textfield',
id: 'loanValueDate', 
name: 'loanValueDate', 
fieldLabel: '审批放款时评估时间',
readOnly:true,  
width:150
}
]},{ columnWidth : .5, 
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'loanValue', 
name: 'loanValue', 
fieldLabel: '审批放款时评估价值（元）',
regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
regexText:'只能输入整数或小数！' , 
readOnly:true, 
width: 150 
}]} 
]},{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
           xtype: 'textfield',
id: 'dealValueDate', 
name: 'dealValueDate', 
fieldLabel: '处置时评估时间',
readOnly:true,  
width:150
}
]},{ columnWidth : .5, 
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'dealValue', 
name: 'dealValue', 
fieldLabel: '处置时评估价值（元）',
regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
regexText:'只能输入整数或小数！' , 
readOnly:true, 
width: 150 
}]} 
]},{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	           xtype: 'datefield',
	id: 'recoverDate', 
	name: 'recoverDate', 
	fieldLabel: '最终收回时间',
	allowBlank: true,  
	readOnly:true,  
	width:150,
	format : 'Y-m-d'
	}
	]},{ columnWidth : .5, 
        layout : "form",
        items : [{
xtype: 'textfield',
id: 'recoverValue', 
name: 'recoverValue', 
fieldLabel: '最终回收金额（元）',
regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
regexText:'只能输入整数或小数！' , 
allowBlank: true,  
readOnly:true,  
width: 150 
}]} 
	]},{
		layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
		           layout : "form",
		           items : [{
		        	   xtype: 'combo', 
						 id : "belongFlag",
			              store :yesOrNoStore,
			              fieldLabel: '所有权属于借款人',
			              valueField : 'code',
			              displayField : 'name',
			              typeAhead : true,
			              width: 150,
			              allowBlank : true,
			              mode:'remote', 
			              editable: false,
			              triggerAction : 'all',
			              selectOnFocus : true,
			              allowBlank: true,
			              readOnly:true,  
			              hiddenName:'belongFlag'
		}
		]},{ columnWidth : .5, 
		           layout : "form",
		           items : [{
		xtype: 'textfield',
		id: 'bankShare', 
		name: 'bankShare', 
		fieldLabel: '该抵押品在我行份额（%）',
		regex:/^[0-9]+(.[0-9]{0,5})?$/,
	 	regexText:'只能输入整数或小数（精确度0.00001）' , 
		allowBlank: true,  
		readOnly:true,  
		width: 150 
		}]} 
		]},{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
       	items : [{
			xtype: 'textfield',
			id: 'share', 
			name: 'share', 
			fieldLabel: '在本合同项下份额（%）',
			regex:/^[0-9]+(.[0-9]{0,5})?$/,
		 	regexText:'只能输入整数或小数（精确度0.00001）' , 
			allowBlank: true,  
			width: 150 
       	}]
	},{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
			
        }]
	}]
}
] 
}]
});

//新增窗口 
addWin = new Ext.Window({ 
id: 'addWin', 
title: '抵质押品', 
width: 720, 
height: 420, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addWin').hide(); } }

] 
});

function addFunction() {

	if(addForm.form.isValid()){
		addForm.form.doAction('submit', { 
			url : 'obligation/addDebtCollateral.do', 
			method : 'post', 
			success : function() { 
			addWin.hide();
			grid.store.reload();
			Ext.Msg.alert('提示', '添加成功！'); 
			}, 
			failure : function() { 
			Ext.Msg.alert('提示', '添加失败！'); 
			} 
		});  
	}
}

//添加按钮单击事件 
function btnAddClick() {
Ext.getCmp("addWin").show(); 
addForm.getForm().getEl().dom.reset(); 
Ext.getCmp("addWin").hide(); 
Ext.getCmp('cId').show(); 
Ext.getCmp('acBtn').show(); 
Ext.getCmp("addWin").buttons[0].handler = addFunction; 
Ext.getCmp("addWin").show(); 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除
function del(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
	Ext.Msg.alert("提示", "请选中要删除的内容"); 
} 
else{
	Ext.MessageBox.confirm('提示', '确实要删除所选的内容吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
 Ext.Ajax.request({
  url:'obligation/deleteDebtCollateral.do',
  params : {collateralId : grid.getSelectionModel().getSelections()[0].data.collateralId},
  method:'POST',
  success:function(){
		Ext.MessageBox.alert("提示","删除成功！");
	   	grid.store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

function editFunction(){
	
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 
        url : 'obligation/editDebtCollateral.do', 
        method : 'post', 
		success : function() {
		 	Ext.getCmp("addWin").hide();
			grid.store.reload();
			Ext.Msg.alert('提示', '保存成功！'); 
		}, 
        failure : function() { 
			Ext.Msg.alert('提示', '保存失败！'); 
 		} 
	}); 
}
}

function checkDetail(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
	Ext.Msg.alert("提示", "请选中要查看的内容！"); 
}
else{
	var selectedId = grid.getSelectionModel().getSelections()[0].data.collateralId;
	var debtNo = grid.getSelectionModel().getSelections()[0].data.debtNo;
           
   	
   	addForm.form.reset(); 
	Ext.getCmp('acBtn').hide();
	addWin.buttons[0].handler = editFunction; 

	Ext.Ajax.request({
		url:'obligation/getDebtCollateral.do',
		params : { collateralId: selectedId, debtNo:debtNo},
		method:'POST',
  		success:function(response){
		 	var json = Ext.util.JSON.decode(response.responseText);
		 	Ext.getCmp("collateralId").setValue(json.collateralId);
		  	Ext.getCmp("collateralName").setValue(json.collateralName);
			Ext.getCmp("collateralTypeLevelOneName").setValue(json.collateralTypeLevelOneName);
			Ext.getCmp("collateralTypeLevelTwoName").setValue(json.collateralTypeLevelTwoName);
		  	Ext.getCmp("collateralTerm").setValue(json.collateralTerm);
		  	Ext.getCmp("endTerm").setValue(json.endTerm);
			Ext.getCmp("nowValue").setValue(json.nowValue);
		   	Ext.getCmp("loanValueDate").setValue(json.loanValueDate);
			Ext.getCmp("loanValue").setValue(json.loanValue);
			Ext.getCmp("dealValueDate").setValue(json.dealValueDate);
		  	Ext.getCmp("dealValue").setValue(json.dealValue);
		  	Ext.getCmp("recoverDate").setValue(json.recoverDate);
		  	Ext.getCmp("recoverValue").setValue(json.recoverValue);
		  	Ext.getCmp("belongFlag").setValue(json.belongFlag);
		  	Ext.getCmp("bankShare").setValue(json.bankShare);
		  	Ext.getCmp("share").setValue(json.share);
		  	
		  	Ext.getCmp('cId').hide();  
              
		  	
			Ext.getCmp("addWin").show(); 
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}

grid.addListener('rowdblclick',checkDetail);

/*
*新增一项抵质押品
*/
var collateralTypeLevelOneStore = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showColTypeLevelOne.do',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'code'}, 
{name:'name'}
]) ,
autoLoad:true
}); 

var collateralTypeLevelTwoStore = new Ext.data.Store({ 
reader:new Ext.data.JsonReader({ },[
{name:'code'}, {name:'name'}
])
});

var addForm2 = new Ext.form.FormPanel({ 
id: 'addForm2', 
width: 650, 
height: 350, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 160, 
items: 
[ { xtype: 'fieldset', 
title: '抵质押品信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[{layout : "form",
items : [{
xtype: 'textfield',
id: 'colName', 
name: 'collateralName', 
fieldLabel: '抵质押品名称',
allowBlank: true,  
width: 180 
}
]},
{
layout : "form",
items : [{
	  xtype: 'combo', 
			id: 'colLevelOneType', 
			name: 'collateralLevelOneType', 
              store :collateralTypeLevelOneStore,
              fieldLabel: '抵质押品类型',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择抵质押品类型...',
              allowBlank : false,
              blankText:'请选择抵质押品类型',
              editable: false,
              typeAhead : true,
              width: 180,
              mode:'local', 
              hiddenName:'collateralLevelOneType',
              triggerAction : 'all',
              selectOnFocus : true,
                      listeners:{        
           select : function(combo, record,index)   
           {   
			var collateralTypeCombo = Ext.getCmp('colType');
            collateralTypeCombo.clearValue(); //可以实现当下拉值变更时，清空之前下拉选项中的值   

            collateralTypeLevelTwoStore.proxy =new Ext.data.HttpProxy({
			url:'showColTypeLevelTwo.do?levelOneCode='+combo.value,
			method:'POST'
			});
            collateralTypeLevelTwoStore.load();
            
           }      
       }
}, {
	  xtype: 'combo', 
			id: 'colType', 
			name: 'collateralType', 
              store :collateralTypeLevelTwoStore,
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择抵质押品类型...',
              allowBlank : false,
              blankText:'请选择抵质押品类型',
              editable: false,
              typeAhead : true,
              width:430,
              mode:'local', 
              hiddenName:'collateralType',
              triggerAction : 'all',
              selectOnFocus : true 
}]
},
{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
           xtype: 'datefield',
			id: 'colTerm', 
			name: 'collateralTerm', 
			fieldLabel: '抵质押品有效起始日',
			allowBlank: true,  
			width:130,
			format : 'Y-m-d'
}
]},
{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
           xtype: 'datefield',
			id: 'endTerm', 
			name: 'endTerm', 
			fieldLabel: '抵质押品有效到期日',
			allowBlank: true,  
			width:130,
			format : 'Y-m-d'
}
]},

]},

{
	layout:"column",
	items:[{ columnWidth : .5, 
	           layout : "form",
	           items : [{
			xtype: 'textfield',
			id: 'colNowValue', 
			name: 'nowValue', 
			fieldLabel: '抵质押品当前评估价值（元）',
			allowBlank: true,  
			width: 130 
	}]} 
]},

{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
           xtype: 'datefield',
id: 'colLoanValueDate', 
name: 'loanValueDate', 
fieldLabel: '审批放款时评估时间',
allowBlank: true,  
width:130,
format : 'Y-m-d'
}
]},{ columnWidth : .5, 
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'colLoanValue', 
name: 'loanValue', 
fieldLabel: '审批放款时评估价值（元）',
allowBlank: true,  
width: 130 
}]} 
]},{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
           xtype: 'datefield',
id: 'colDealValueDate', 
name: 'dealValueDate', 
fieldLabel: '处置时评估时间',
allowBlank: true,  
width:130,
format : 'Y-m-d'
}
]},{ columnWidth : .5, 
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'colDealValue', 
name: 'dealValue', 
fieldLabel: '处置时评估价值（元）',
allowBlank: true,  
width: 130 
}]} 
]},{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	           xtype: 'datefield',
	id: 'calRecoverDate', 
	name: 'recoverDate', 
	fieldLabel: '最终收回时间',
	allowBlank: true,   
	width:130,
	format : 'Y-m-d'
	}
	]},{ columnWidth : .5, 
        layout : "form",
        items : [{
xtype: 'textfield',
id: 'calRecoverValue', 
name: 'recoverValue', 
fieldLabel: '最终回收金额（元）',
allowBlank: true,  
width: 130 
}]} 
	]},{
		layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
		           layout : "form",
		           items : [{
		        	   xtype: 'combo', 
						 id : "calBelongFlag",
			              store :yesOrNoStore,
			              fieldLabel: '所有权属于借款人',
			              valueField : 'code',
			              displayField : 'name',
			              typeAhead : true,
			              width: 130,
			              allowBlank : true,
			              mode:'remote', 
			              editable: false,
			              triggerAction : 'all',
			              selectOnFocus : true,
			              allowBlank: true, 
			              hiddenName:'belongFlag'
		}
		]},{ columnWidth : .5, 
		           layout : "form",
		           items : [{
		xtype: 'textfield',
		id: 'calBankShare', 
		name: 'bankShare', 
		fieldLabel: '该抵押品在我行份额',
		allowBlank: true, 
		width: 130 
		}]} 
		]},
		{
			layout : "form",
	        items : [{
				xtype: 'textarea',
				id: 'otherInfo', 
				name: 'otherInfo', 
				fieldLabel: '其他说明（限100汉字以内）',
				allowBlank: true,  
				width: 440 
	        }]
		},
] 
}]
});


addWin2 = new Ext.Window({ 
id: 'addWin2', 
title: '新增抵质押品', 
width: 680, 
height: 440, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm2, 
buttons: 
[  
{ text: '保存' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addWin2').hide(); } }

] 
});

function addCol(){
	Ext.getCmp('addForm2').form.reset(); 
	Ext.getCmp("addWin2").buttons[0].handler = function(){
		if(addForm2.form.isValid()){
		addForm2.form.doAction('submit', { 
			url : 'obligation/addCollateral.do', 
			method : 'post', 
			success : function() { 
				Ext.getCmp('addWin2').hide();
				Ext.Msg.alert('提示', '添加成功！'); 
				collateralStore.reload();
			}, 
			failure : function() { 
				Ext.Msg.alert('提示', '添加失败！'); 
			} 
		});  
	}
	}; 
	Ext.getCmp("addWin2").show(); 
}

}); 